<?php
class Agitum_Model_Tender extends Agitum_Model_Object
{
	protected $_t_class = 'Agitum_Model_DbTable_Tender';
	
	private $_tour = null;
	private $_country = null;

	public function setTour( $tour ) 
	{
		$this->_tour = $tour;
	}
	
	public function setCountry( $country ) 
	{
		$this->_country = $country;
	}
	
	public function save() 
	{
		parent::save();
		foreach ($this->_tour as $i => $tour ) {
			foreach ( $tour as $j => $tour_id ) {
				$t = new Agitum_Model_Tender_Tour();
				$t->src_id = $this->id;
				$t->dst_id = $tour_id;
				$t->proposal = $i;
				$t->save();
			}
		}
		foreach ($this->_country as $i => $country ) {
			foreach ( $country as $j => $country_id ) {
				$t = new Agitum_Model_Tender_Country();
				$t->src_id = $this->id;
				$t->dst_id = $country_id;
				$t->proposal = $i;
				$t->save();
			}
		}
	}
	
	public function getActiveList($param) 
	{
		$db = Zend_Registry::get('Zend_Db');
		
		/*
		(SELECT count(id) FROM offer WHERE tender_id = a.id GROUP BY tender_id) as offer_count,
    	(SELECT min(price) FROM offer WHERE tender_id = a.id) as minimal_price,
    	(SELECT price FROM offer WHERE tender_id = a.id ORDER BY created LIMIT 1) as first_price
		 */
		
		$select1 = $db->select()
			->from('offer', array(new Zend_Db_Expr('count(id)')))
			->where('tender_id = a.id')
			->group('tender_id');
	
		$select2 = $db->select()
			->from('offer', array(new Zend_Db_Expr('min(price)')))
			->where('tender_id = a.id');
		
		$select3 = $db->select()
			->from('offer', array(new Zend_Db_Expr('price')))
			->where('tender_id = a.id')
			->order('created')
			->limit(1);
		
		$select = $db->select()
				->from( array('a'=>'tender'), 
						array(
							'a.id',
							'timeto' => new Zend_Db_Expr('a.stop - now()'),
							'offer_count' => new Zend_Db_Expr('('.$select1.')'),
							'minimal_price' => new Zend_Db_Expr('('.$select2.')'),
							'first_price' => new Zend_Db_Expr('('.$select3.')'),
							'owner_id'
						)
				);

		if( $param['country'] ) $select->joinCross( array('c' => 'tender_country'), array() );
		if( $param['tour'] ) $select->joinCross( array('d' => 'tender_tour'), array() );
				
		$select->joinLeft( array('b' => 'geo'), 'b.id = a.city_id', array('city' => 'title'))
				->where('a.start <= '.new Zend_Db_Expr('NOW()'))
				->where('a.stop >= '.new Zend_Db_Expr('NOW()'));
				
		if ( $param['city'] ) { 
			$select->where('a.city_id = ?', $param['city']);
		}
		
		if ( $param['country'] ) {
			$select->where('c.dst_id = ?', $param['country'])
					->where('c.src_id = a.id');
		}
										
		if ( $param['tour'] ) {
			$select->where('d.dst_id = ?', $param['tour'])
					->where('d.src_id = a.id');
		}
		
		$select->order('a.start desc');
		return $select->query()->fetchAll();
	}
	
	public function getActiveTimes($id_list) 
	{
	    $db = Zend_Registry::get('Zend_Db');
	    $select = $db->select()
				->from( array('a'=>'tender'), array('a.id', 'timeto' => new Zend_Db_Expr('a.stop - now()')))
				->where( new Zend_Db_Expr('id IN ('.implode(',', $id_list).')'));
		return $select->query()->fetchAll();
	}
}